package blocstac.editimage.zoom
{
	import blocstac.editimage.EditState;
	import blocstac.editimage.base.StateManager;
	import blocstac.editimage.rotate.RotateComponent;
	
	import flash.events.MouseEvent;
	
	import spark.components.HSlider;

public class ZoomComponent extends HSlider
{
	private var _stateManager:StateManager
	private var _rotateComponent:RotateComponent
	
	function ZoomComponent(stateManager:StateManager, rotateComponent:RotateComponent) {
		_stateManager = stateManager
		_rotateComponent = rotateComponent
	}
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	override protected function createChildren():void {
		super.createChildren()
		
		minimum = 100
		maximum = 400
		value = 100
		stepSize = 20
		snapInterval = 10
		setStyle("liveDragging", true)
		width= 100
		height = 11
		 
		dataTipPrecision = 0
		dataTipFormatFunction = dataTipFormatter
	}
	
	
	/**
	 * 
	 * @param minimum 1 corresponds to a 100% of the image
	 */
	public function setMinimumAndValueTo(minimum:Number):void {
		var newMinimum:Number = minimum * 100
		this.minimum = newMinimum
		this.value = newMinimum
	}
	
	private function dataTipFormatter(value:Number):Object { 
		return value + "%" 
	}
	
	public function updateEditState():void {
		visible = false
		if (_stateManager.editState.isZoomable) {
			value = _stateManager.viewModel.userViewScale
			_rotateComponent.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler) 
			addEventListener(MouseEvent.ROLL_OVER, zoomRollOverHandler)
		} else {
			_rotateComponent.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler)
			_rotateComponent.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler)
			removeEventListener(MouseEvent.ROLL_OVER, zoomRollOverHandler)
			removeEventListener(MouseEvent.ROLL_OUT, zoomRollOutHandler)
		}
	}
	
	private function rollOverHandler(event:MouseEvent):void {
		visible = true
		_rotateComponent.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler)
		_rotateComponent.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler)
	}
	
	private function rollOutHandler(event:MouseEvent):void {
		visible = false
		_rotateComponent.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler)
		_rotateComponent.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler) 
	}
	
	private function zoomRollOverHandler(event:MouseEvent):void {
		visible = true
		removeEventListener(MouseEvent.ROLL_OVER, zoomRollOverHandler)
		addEventListener(MouseEvent.ROLL_OUT, zoomRollOutHandler)
	}
	
	private function zoomRollOutHandler(event:MouseEvent):void {
		visible = false
		removeEventListener(MouseEvent.ROLL_OUT, zoomRollOutHandler)
		addEventListener(MouseEvent.ROLL_OVER, zoomRollOverHandler) 
	}
}
}